package com.zhao.modleclass.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

import com.zhao.modleclass.R;

import java.util.Timer;
import java.util.TimerTask;

public class FirstGuidepage extends AppCompatActivity {
    private Context mContext=this;
    private ImageView iv_firstGuidepage,iv_secondGuidepage;
    private String url="https://mrobot.pclady.com.cn/modern/x/modern.pclady.com.cn/app/common/launch.do";
    private Button startBtn;
    private Handler handler ;
    private Runnable runnable;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_guidepage);
        //设置状态栏的颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            //获取样式中的属性值
            TypedValue typedValue = new TypedValue();
            this.getTheme().resolveAttribute(Color.parseColor("#FF4081"), typedValue, true);
            int[] attribute = new int[]{Color.parseColor("#FF4081")};
            TypedArray array = this.obtainStyledAttributes(typedValue.resourceId, attribute);
            int color = array.getColor(0, Color.parseColor("#FF4081"));
            array.recycle();
            window.setStatusBarColor(color);
        }
        iv_firstGuidepage= (ImageView) findViewById(R.id.iv_firstGuidepage);
        startBtn= (Button) findViewById(R.id.button_start);
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,MainActivity.class);
                mContext.startActivity(intent);
                //让activity死亡
                finish();
            }
        });

        Timer timer=new Timer();
        timer.schedule(task,2000);
//        init();
    }

    private void init() {
        handler = new Handler();
        runnable=new Runnable() {
            @Override
            public void run() {
                SharedPreferences sha=FirstGuidepage.this.getSharedPreferences("share",MODE_PRIVATE);
                boolean isFirstRun = sha.getBoolean("isFirstRun",true);
                SharedPreferences.Editor editor=sha.edit();
                //第一次安装  执行引导页
                if (isFirstRun){
                    editor.putBoolean("isFirstRun", false);
                    editor.commit();
//                    Intent intent = new Intent(FirstGuidepage.this, GuideActivity.class);
//                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                    startActivity(intent);
                } else {
                    Intent intent = new Intent(FirstGuidepage.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                }
                FirstGuidepage.this.finish();
            }
        };
        handler.postDelayed(runnable, 1500);

    }

//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        handler.removeCallbacks(runnable);
//    }
    //延时器
    TimerTask task=new TimerTask() {
        @Override
        public void run() {
            Intent intent=new Intent(mContext,MainActivity.class);
            mContext.startActivity(intent);
            //让activity死亡
            finish();
        }
    };
}
